<?php
namespace Home\Controller;
use Think\Controller;
use Com\Wxpay\lib\WxPayConfig;
use Com\Wxpay\example\JsApiPay;

// +----------------------------------------------------------------------
// |===== Pyboon =====
// +----------------------------------------------------------------------
// | Copyright (c) 2016 Pyboon All rights reserved.
// +----------------------------------------------------------------------
// | Author: Baboon <Pyboon@foxmail.com>
// +----------------------------------------------------------------------
// | Date: 2016年2月18日 下午4:22:11
// +----------------------------------------------------------------------
class CheckInController extends Controller{
    
    
    public function generateCode(){
    
        if(IS_POST){
            $return_data = array();
            $ClassID = I("ClassID");
            $ClassTeacherName = I("ClassTeacherName");
            $pwd = I("pwd");
            $model = D("FMSCommon");
            $model->setTableName("FBase_ClassInfo");
            $class = $model->where(array("Id"=>$ClassID))->find();
            if($class){
                if($class["ClassTeacherName"]==$ClassTeacherName){
                    $model->setTableName("gUser");
                    $teacher = $model->where(array("Id"=>$class["ClassTeacherID"]))->find();
                    if($teacher["Password"] == strtoupper(md5($pwd))){
                        $data = 'http://'.$_SERVER['HTTP_HOST']."/sdcenWX/index.php/Home/CheckIn/info/ClassID/".$ClassID;
                        qrcode($data, $ClassID.".png");
                        $return_data["status"] = 200;
                        $return_data["url"] = "/sdcenWX/Public/Img/".$ClassID.".png";
                        $return_data["message"] = "success";
                    }
                    else {
                        $return_data["status"] = 302;
                        $return_data["message"] = "班主任密码不正确";
                    }
                    
                }
                else {
                    $return_data["status"] = 301;
                    $return_data["message"] = "班主任信息不正确";
                }
                
            }
            else {
                $return_data["status"] = 300;
                $return_data["message"] = "无此班级";
            }
            $this->ajaxReturn($return_data);
            
        }
        else {
            $model = D("FMSCommon");
            $model->setTableName("FBase_ClassInfo");
            $class = $model->where("IsReport=1")->select();
            $this->assign("class",$class);
            $this->display();
        }
    
    }
    public function index(){//报到输入cardNum界面
     //接收班级Id，查询显示班级信息
     //header("Content-type: text/html; charset=utf-8");
     $ClassID = I("ClassID");//"a6c1941d-87bd-4e94-b532-6556b9c986cd";
     $model = D("FMSCommon");
     $model->setTableName("FBase_ClassInfo");
     $class = $model->where(array("Id"=>$ClassID))->find();
     
     $openid = "453ajgi3q58tjdfe";
     $this->assign("openid",$openid);
     $this->assign("ClassID",$ClassID);
     $this->assign("class",$class);
     $this->display();
    } 
    
    
    public function info(){
        
        $openid = $this->getOpenid();
        $model = D("FMSCommon");
        $model->setTableName("FBase_StuInfo");
        $data["WeiXin"] = $openid;
        $res = $model->where($data)->find();
        $ClassID = I("ClassID");
        if($res){
            $this->assign("openid",$openid);
            
            $model->setTableName("View_Base_StuInfo");
            $stu = $model->where(array("WeiXin"=>$openid,"ClassID"=>$ClassID))->find();
            if($stu){
                //该班存在该学生
                $this->assign("stu",$stu);
            }
            else {
                //绑定过的用户，但是本班级没有该学员
            }
        }
        else {
            //$url = $_SERVER['HTTP_HOST']."/sdcenWX/index.php/Home/CheckIn/info?classid=".$ClassID;
            //session("classid",$ClassID);
            $this->redirect('/Home/CheckIn/bind', array('classid' => $ClassID));
            //$this->redirect($url);
        }
        $this->display();
    }
    
    
    public function checkin(){
        $openid = I("openid");
        $ClassID = I("ClassID");
        $StuID = I("StuID");
        $model = D("FMSCommon");
        $return_data = array();
        $model->setTableName("FBase_ClassStuInfo");
        $data["IsReport"] = 1;
        $data["ReportDate"] = date("Y-m-d H:i:s");
        $stu = $model->where(array("ClassID"=>$ClassID,"StuID"=>$StuID))->save($data);
      /*   print_r($ClassID);
        print_r($model->getLastSql()); */
        if($stu){
            
            $return_data["status"] = 200;
            $return_data["url"] = U("Home/CheckIn/success");
            $return_data["message"] = "success";
            $this->ajaxReturn($return_data);
        }
        else {
            $return_data["status"] = 300;
            $return_data["message"] = "fail";
            $this->ajaxReturn($return_data);
        }
    }
    
    public function success(){
        $this->display();
    }
    /**
    * @description:
    * @params:报到统计
    * @return:
    * @author: baboon
    **/
    public function statistic(){
        if(IS_POST){
            $ClassID = I("ClassID");//"a6c1941d-87bd-4e94-b532-6556b9c986cd";
            $ClassTeacherName = I("ClassTeacherName");
            $pwd = I("pwd");
            $model = D("FMSCommon");
            $model->setTableName("FBase_ClassInfo");
            $class = $model->where(array("Id"=>$ClassID))->find();
            if($class){
                if($class["ClassTeacherName"]==$ClassTeacherName){
                    $model->setTableName("gUser");
                    $teacher = $model->where(array("Id"=>$class["ClassTeacherID"]))->find();
                    if($teacher["Password"] == strtoupper(md5($pwd))){
                        $model->setTableName("View_Base_StuInfo");
                        $stu = $model->where(array("ClassID"=>$ClassID))->order("IsReport desc")->select();
                        $this->assign("sum",count($stu));
                        $sum = $model->where(array("ClassID"=>$ClassID))->field(array("IsReport", "count(IsReport)"=>"sum"))->group('IsReport')->select();
                        if($sum[0]["IsReport"]==0){
                            $noReport = $sum[0]["sum"];
                            $this->assign("noReport",$noReport);
                            $this->assign("hasReport",count($stu)-$noReport);
                        }
                        else if($sum[0]["IsReport"]==1){
                            $hasReport = $sum[0]["sum"];
                            $this->assign("hasReport",$hasReport);
                            $this->assign("noReport",count($stu)-$noReport);
                        }
                        $this->assign("stus",$stu);
                    }
                    else {
                        $this->assign("err",3);
                    }
                    
                }
                else {
                    $this->assign("err",1);
                }
            }
            else {
                $this->assign("err",2);
            }
            
            
          
            $this->display();
        }
        else {
            $model = D("FMSCommon");
            $model->setTableName("FBase_ClassInfo");
            $class = $model->where("IsReport=1")->select();
            $this->assign("class",$class);
            $this->display("statisticLogin");
        }
        
    }
    
    
    public function bind(){
        if(IS_POST){
            $return_data = array();
            $CardNum = I("CardNum");
            $openid = I("openid");
            /* $classID = session("classid");
            $this->assign("classid",$classID); */
            if(!empty($CardNum) && !empty($openid)){
                $model = D("FMSCommon");
                $model->setTableName("FBase_StuInfo");
                $data["WeiXin"] = $openid;
                $stu = $model->where(array("CardNum"=>$CardNum))->save($data);
                if($stu){
                
                    $return_data["status"] = 200;
                    $return_data["message"] = "success";
                    $classID = I("classid");
                    if(!empty($classID)){
                        $url = $_SERVER['HTTP_HOST']."/sdcenWX/index.php/Home/CheckIn/info/ClassID/".$classID;
                        $return_data["url"] = $url;
                    }
                    else {
                        $return_data["url"] = U("Home/CheckIn/bind");
                    }
                }
                else {
                    $return_data["status"] = 301;
                    $return_data["message"] = "fail";
                }
            }
            else {
                $return_data["status"]= 300;
                $return_data["message"] = "No openid or No cardNum";
            }
             $this->ajaxReturn($return_data);   
            
        }
        else {
            $openid = $this->getOpenid();
            $model = D("FMSCommon");
            $model->setTableName("View_Base_StuInfo");
            $stu = $model->where(array("weixin"=>$openid))->select();
            if($stu){
                $this->assign("isBind",true);
                $this->assign("user",$stu[0]);
                $this->assign("proj",$stu);
                $this->assign("openid",$openid);
            }
            else {
                $this->assign("isBind",false);
                $this->assign("openid",$openid);
            }
            $this->display();
        }
        
        
    }
    
    
    public function unbind(){
        $openid = I("openid");
        $return_data = array();
        if(!empty($openid)){
            $model = D("FMSCommon");
            $model->setTableName("FBase_StuInfo");
            $user = $model->where(array("WeiXin"=>$openid))->find();
            if($user){
                $model->where(array("WeiXin"=>$openid))->setField("WeiXin",'');
                $return_data["status"] = 200;
                $return_data["message"] = "解绑成功";
                $return_data["url"] = U("Home/CheckIn/bind");
            }
            else {
                $return_data["status"] = 301;
                $return_data["message"] = "无user";
            }
    
        }
        else {
            $return_data["status"] = 300;
            $return_data["message"] = "无openid";
        }
        $this->ajaxReturn($return_data);
    }
    
    protected function getOpenid(){
        if(!empty($_SESSION["openid"]))
        {
            return $_SESSION["openid"];
        }
        else {
            $options = array(
                'appid'=>C("Non_Academic_WXAPPID"),
                'secret'=>C("Non_Academic_WXAPPSECRET"),
            );
            $wxpaycfg = new WxPayConfig($options);
            $tools = new JsApiPay($wxpaycfg);
            $openid = $tools->GetOpenid();
            session("openid",$openid);
            return $openid;
        }
        
    }
    /**
    * @description:截取图片函数
    * @params:
    * @return:
    * @author: baboon
    **/
    public function img(){
        
        $this->display();
    }
    
    
   
}